package cn.edu.seu.cose.qol.ws.resource.util.adapter;

import java.util.Collection;
import java.util.LinkedList;

import cn.edu.seu.cose.qol.model.dto.question.QuestionContent;
import cn.edu.seu.cose.qol.model.dto.question.QuestionContentItem;
import cn.edu.seu.cose.qol.model.dto.question.QuestionContentType;
import cn.edu.seu.cose.qol.model.dto.question.QuestionInstance;
import cn.edu.seu.cose.qol.model.po.question.Choice;
import cn.edu.seu.cose.qol.model.po.question.ShortAnswer;
import cn.edu.seu.cose.qol.model.po.question.TrueOrFalse;


public class QuestionAdapter {
	
	private static QuestionAdapter _instance;
	
	public static QuestionAdapter getInstance() {
		if (_instance == null) {
			_instance = new QuestionAdapter();
		}
		return _instance;
	}
	
	protected QuestionAdapter() {}
	
	public QuestionInstance adapt(TrueOrFalse torf) {
		String title = torf.getQuestionTitle();
		long id = torf.getTrueOrFalseId();
		QuestionContentType type = QuestionContentType.TRUE_OR_FALSE;
		
		QuestionInstance questionInstance = new QuestionInstance();
		questionInstance.setId(id);
		questionInstance.setContentType(type);
		questionInstance.setBody(title);
		questionInstance.setContent(null);
		
		return questionInstance;
	}
	
	public QuestionInstance adapt(Choice choice) {
		long id = choice.getChoiceId();
		QuestionContentType type = QuestionContentType.CHOICE;
		String title = choice.getQuestionTitle();
		int max = choice.getMaxChoiceNumber();
		Collection<String> options = choice.getOptions();

		QuestionInstance questionInstance = new QuestionInstance();
		questionInstance.setId(id);
		questionInstance.setContentType(type);
		questionInstance.setBody(title);
		
		LinkedList<QuestionContentItem> list =
				new LinkedList<QuestionContentItem>();
		
		int counter = 0;
		for (String option: options) {
			QuestionContentItem item = new QuestionContentItem();
			item.setId(counter);
			item.setText(option);
			list.add(item);
			
			++counter;
		}
		
		QuestionContent content = new QuestionContent();
		content.setMaxChoiceNumber(max);
		content.setItems(list.toArray(new QuestionContentItem[0]));
		questionInstance.setContent(content);
		
		return questionInstance;
	}
	
	public QuestionInstance adapt(ShortAnswer shortAnswer) {
		long id = shortAnswer.getShortAnswerId();
		String title = shortAnswer.getQuestionTitle();
		QuestionContentType type = QuestionContentType.SHORT_ANSWER;
		
		QuestionInstance questionInstance = new QuestionInstance();
		questionInstance.setId(id);
		questionInstance.setContentType(type);
		questionInstance.setBody(title);
		questionInstance.setContent(null);
		
		return questionInstance;
	}
	
	public TrueOrFalse adaptToTrueOrFalse(
			QuestionInstance questionInstance) {
		if (!QuestionContentType.TRUE_OR_FALSE.equals(
				questionInstance.getContentType())) {
			throw new IllegalArgumentException("content type not supported");
		}
		
		long id = questionInstance.getId();
		String body = questionInstance.getBody();
		
		TrueOrFalse torf = new TrueOrFalse();
		torf.setTrueOrFalseId(id);
		torf.setQuestionTitle(body);
		return torf;
	}
	
	public Choice adaptToChoice(
			QuestionInstance questionInstance) {
		if (!QuestionContentType.CHOICE.equals(
				questionInstance.getContentType())) {
			throw new IllegalArgumentException("content type not supported");
		}
		
		long id = questionInstance.getId();
		String body = questionInstance.getBody();
		QuestionContent content = questionInstance.getContent();
		QuestionContentItem[] items = content.getItems();
		int max = content.getMaxChoiceNumber();
		
		LinkedList<String> options = new LinkedList<String>();
		for (QuestionContentItem item: items) {
			options.add(item.getText());
		}
		
		Choice choice = new Choice();
		choice.setQuestionTitle(body);
		choice.setChoiceId(id);
		choice.setMaxChoiceNumber(max);
		choice.setOptions(options);
		
		return choice;
	}
	
	public ShortAnswer adaptToShortAnswer(
			QuestionInstance questionInstance) {
		if (!QuestionContentType.SHORT_ANSWER.equals(
				questionInstance.getContentType())) {
			throw new IllegalArgumentException("content type not supported");
		}
		
		long id = questionInstance.getId();
		String body = questionInstance.getBody();
		
		ShortAnswer shortAnswer = new ShortAnswer();
		shortAnswer.setShortAnswerId(id);
		shortAnswer.setQuestionTitle(body);
		
		return shortAnswer;
	}

}
